<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增或修改页</title>
    <style>
        body{
            border: 1px solid;
            width: 250px;
            height: 195px;
        }
    </style>
</head>
<body>
        添加博客或者修改 
    <table>
        <tr>
            <td><label for="">标题：</label></td>
            <td><input type="text" name="biaoti"></td>
        </tr>
        <tr>
            <td><label for="">摘要：</label></td>
            <td><input type="text" name="zaiyao"></td>
        </tr>
        <tr>
            <td><label for="">内容：</label></td>
            <td><input type="text" name="neirong"></td>
        </tr>
        <tr>
            <td><label for="">分类：</label></td>
            <td><input type="text" name="fenlei"></td>
        </tr>
        <tr>
            <td><label for="">作者：</label></td>
            <td><input type="text" name="zuozhe"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消" onclick="cancel()"></td>
        </tr>
        <script src="./js/jquery-3.6.0.min.js"></script>
        <script src="./js/api.js"></script>
        <script>
            $(function(){
                let id=getUrlParam('id');
                getBlogById(id).then(res=>{
                    console.log(res);
                    let obj=res.data;
                    $('[name=biaoti]').val(obj.biaoti);
                    $('[name=zaiyao]').val(obj.zaiyao);
                    $('[name=neirong]').val(obj.neirong);
                    $('[name=fenlei]').val(obj.fenlei);
                    $('[name=zuozhe]').val(obj.zuozhe);
                })
            })

             //获取url中的参数 其中name指的是url中的键的名称
            function getUrlParam(name) {

            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象

            var r = window.location.search.substr(1).match(reg); //匹配目标参数

            if (r != null) {
                return unescape(r[2]);
            } else {
                return null; //返回参数值
            }

            }


            function save(){
                let id=getUrlParam('id');
                let obj={
                    biaoti: $('[name=biaoti]').val(),
                    zaiyao: $('[name=zaiyao]').val(),
                    neirong: $('[name=neirong]').val(),
                    fenlei: $('[name=fenlei]').val(),
                    zuozhe: $('[name=zuozhe]').val(),
                }
                if(id){
                    updateBlog(id, obj).then(res=>{
                        location.href='./index.html';
                    })
                }else{
                    let params={
                        biaoti:obj.biaoti,
                        zaiyao:obj.zaiyao,
                        neirong:obj.neirong,
                        fenlei:obj.fenlei,
                        zuozhe:obj.zuozhe,
                    }
                    addBlog(params).then(res=>{
                        location.href='./index.html';
                    })
                }
            }


            function cancel(){
                location.href='./index.html';
            }
        </script>
    </table>
</body>
</html>